package com.geekaca.news.service.impl;

import com.geekaca.news.domain.NewsCategory;
import com.geekaca.news.mapper.NewsCategoryMapper;
import com.geekaca.news.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private NewsCategoryMapper newsCategoryMapper;

    @Override
    public int getTotalCategories() {
        return newsCategoryMapper.getTotalCategories();
    }

    @Override
    public List<NewsCategory> getCategoryList(Integer page, Integer limit) {
        int start = (page - 1) * limit;
        int pageSize = limit;
        return newsCategoryMapper.getAllCategory(start, pageSize);
    }

    @Override
    public List<NewsCategory> getAllCategories(Integer page, Integer limit) {
        Integer start = 0;
        if (page == null) {
            start = null;
        } else {
            start = (page - 1) * limit;

        }
        Integer pageSize = limit;
        return newsCategoryMapper.getAllCategory(start, pageSize);
    }

    @Override
    public int updateCategory(Integer categoryId, String categoryName, String categoryIcon) {
        return newsCategoryMapper.udateCatgory(categoryId, categoryName, categoryIcon);
    }

    @Override
    public int deleteCategory(Integer id) {
        return newsCategoryMapper.updateDelCatById(id);
    }

    @Override
    public int insertCategory(NewsCategory category) {
        return newsCategoryMapper.insert(category);
    }
}
